//
//  KaiJuFaPiaoViewController.m
//  DistributorAB
//
//  Created by 李 on 2019/12/9.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "KaiJuFaPiaoViewController.h"
#import "KaiJuFaPiaoSelectTypeCell.h"
#import "KaiJuFaPiaoInfoCell.h"

@interface KaiJuFaPiaoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *listTableView;
@property (nonatomic,assign) NSInteger faPiaoSelectedType;
@property (nonatomic,assign) NSInteger faPiaoInfoType;
@property (nonatomic,strong) FaPiaoInfoParamsModel *faPiaoInfoParamsModel;

@end

static NSString *const KaiJuFaPiaoSelectTypeCellID = @"KaiJuFaPiaoSelectTypeCell";
static NSString *const KaiJuFaPiaoInfoCellID = @"KaiJuFaPiaoInfoCell";

@implementation KaiJuFaPiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"开具发票";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.faPiaoSelectedType = 0;
    [self configViews];
    self.faPiaoInfoParamsModel = [[FaPiaoInfoParamsModel alloc]init];
    //初始 默认不开发票
    //是否开发票0不开1开
    self.faPiaoInfoParamsModel.invoice = @"0";
}
-(void)configViews
{
    [self.listTableView registerNib:[UINib nibWithNibName:NSStringFromClass([KaiJuFaPiaoSelectTypeCell class]) bundle:nil] forCellReuseIdentifier:KaiJuFaPiaoSelectTypeCellID];
    [self.listTableView registerNib:[UINib nibWithNibName:NSStringFromClass([KaiJuFaPiaoInfoCell class]) bundle:nil] forCellReuseIdentifier:KaiJuFaPiaoInfoCellID];
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 48)];
    footerView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(27, 0, ScreenW-56, 48)];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    sureBtn.titleLabel.textColor =[UIColor colorWithHexString:@"#FFFFFF"];
    sureBtn.backgroundColor =[UIColor colorWithHexString:@"#C6A47E"];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    sureBtn.layer.cornerRadius = 4;
    [footerView addSubview:sureBtn];
    
    self.listTableView.tableFooterView = footerView;
    [self.view addSubview:self.listTableView];
}
-(void)sureBtnClick
{
    //不开发票
    if ([self.faPiaoInfoParamsModel.invoice isEqualToString:@"0"]) {
        self.faPiaoInfoParamsModel.invoice = @"0";
        self.faPiaoInfoParamsModel.invoiceType = @"";
        self.faPiaoInfoParamsModel.title= @"";
        self.faPiaoInfoParamsModel.phone= @"";
        self.faPiaoInfoParamsModel.email= @"";
    }
    else
    {
        if (self.faPiaoInfoParamsModel.email.length==0)
        {
            ShowToastWithText(@"请输入电子邮箱");
            return;
        }
        else
        {
            //个人 发票
            if ([self.faPiaoInfoParamsModel.invoiceType isEqualToString:@"1"])
            {
                //去除字符串中空格
                NSString *phone = [self.faPiaoInfoParamsModel.phone stringByReplacingOccurrencesOfString:@" " withString:@""];
                if (![HFTools isMobileNumberOnly:phone]) {
                    ShowToastWithText(@"请输入正确电话号码");
                    return;
                }
            }
            //公司 发票
            else if ([self.faPiaoInfoParamsModel.invoiceType isEqualToString:@"2"]) {
                if (self.faPiaoInfoParamsModel.title.length==0)
                {
                    ShowToastWithText(@"请输入发票抬头");
                    return;
                }
                //识别号
                if (self.faPiaoInfoParamsModel.phone.length==0)
                {
                    ShowToastWithText(@"请输入识别号");
                    return;
                }
            }
            if (![HFTools isValidateEmail:self.faPiaoInfoParamsModel.email]) {
                ShowToastWithText(@"请输入正确的电子邮箱");
                return;
            }
        }
    }
    if (self.faPiaoInfoHandle) {
        self.faPiaoInfoHandle(self.faPiaoInfoParamsModel);
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //不开具发票 显示1行
    if (self.faPiaoSelectedType ==0) {
        return 1;
    }
    return 2;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        KaiJuFaPiaoSelectTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:KaiJuFaPiaoSelectTypeCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        __weak typeof(cell)weakCell = cell;
        cell.btnClickBlock = ^(NSInteger index) {
            self->_faPiaoSelectedType = index;
            if (index == 0) {
                weakCell.buKaiFaPiaoBtn.selected = YES;
                weakCell.dianZiFaPiaoBtn.selected = NO;
                self.faPiaoInfoParamsModel.invoice = @"0";
            }
            else
            {
                weakCell.buKaiFaPiaoBtn.selected = NO;
                weakCell.dianZiFaPiaoBtn.selected = YES;
                self.faPiaoInfoParamsModel.invoice = @"1";
                self.faPiaoInfoParamsModel.invoiceType = @"1";
            }
            [self.listTableView reloadData];
        };
        return cell;
    }
    else if (indexPath.row == 1)
    {
        KaiJuFaPiaoInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:KaiJuFaPiaoInfoCellID];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        __weak typeof(cell)weakCell = cell;
        
        cell.infoTypeBtnClickBlock = ^(NSInteger index) {
            self->_faPiaoInfoType = index;
            weakCell.taiTouTF.text = nil;
            weakCell.phoneOrShuiHaoTF.text = nil;
            weakCell.emailTF.text = nil;
            //发票类型1个人2公司
            if (index==0) {
                self.faPiaoInfoParamsModel.invoiceType = @"1";
                self.faPiaoInfoParamsModel.title= @"";
                self.faPiaoInfoParamsModel.phone= @"";
                self.faPiaoInfoParamsModel.email= @"";
            }
            else if (index==1) {
                self.faPiaoInfoParamsModel.invoiceType = @"2";
                self.faPiaoInfoParamsModel.title= @"";
                self.faPiaoInfoParamsModel.phone= @"";
                self.faPiaoInfoParamsModel.email= @"";
            }
            [self.listTableView reloadData];
        };
        cell.taiTouTFHandle = ^(NSString * _Nullable text) {
            self.faPiaoInfoParamsModel.title = text;
        };
        cell.phoneOrShuiHaoTFHandle = ^(NSString * _Nullable text) {
            self.faPiaoInfoParamsModel.phone = text;
        };
        cell.emailTFHandle = ^(NSString * _Nullable text) {
            self.faPiaoInfoParamsModel.email = text;
        };
        //发票类型 选择个人 显示为 发票抬头 手机号
        if (self.faPiaoInfoType==0) {
            cell.taiTouTF.text = @"个人（不可修改）";
            cell.taiTouTF.enabled = NO;
            self.faPiaoInfoParamsModel.title = @"个人";
            NSString *text = @"*手机号";
            NSMutableAttributedString *attStr = [NSString getAttributedStringWithOriginalString:text originalColor:[UIColor colorWithHexString:@"#666666"] originalFont:FontSize(14) originalLineSpacing:0 changeString:@"*" changeColor:[UIColor colorWithHex:0xF42850] changeFont:FontSize(14)];
            cell.phoneOrShuiHaoWordsLab.attributedText = attStr;
            cell.phoneOrShuiHaoTF.placeholder = @"请输入手机号";
        }
        //发票类型 选择公司 显示为 发票抬头 识别号
        if (self.faPiaoInfoType==1) {
            cell.taiTouTF.enabled = YES;
            cell.taiTouTF.placeholder = @"请输入发票抬头";
            NSString *text =@"*识别号";
            NSMutableAttributedString *attStr = [NSString getAttributedStringWithOriginalString:text originalColor:[UIColor colorWithHexString:@"#666666"] originalFont:FontSize(14) originalLineSpacing:0 changeString:@"*" changeColor:[UIColor colorWithHex:0xF42850] changeFont:FontSize(14)];
            cell.phoneOrShuiHaoWordsLab.attributedText = attStr;
            cell.phoneOrShuiHaoTF.placeholder = @"请输入识别号";
        }
        return cell;
    }
    NSString *cellIdentifier = @"iden";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //不开具发票 显示1行
    if (self.faPiaoSelectedType ==0) {
        if (indexPath.row ==0) {
            return 140;
        }
    }
    else
    {
        if (indexPath.row ==0) {
            return 140;
        }
        if (indexPath.row ==1) {
            return 343;
        }
    }
    return 0;
}
- (UITableView *)listTableView
{
    if (!_listTableView) {
        _listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,kNaviHeight, ScreenW, ScreenH-kNaviHeight-TabbarSafeBottomMargin) style:UITableViewStylePlain];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.backgroundColor = [UIColor colorWithHex:0xF2F2F2];
        _listTableView.estimatedSectionHeaderHeight = 0;
        _listTableView.estimatedSectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _listTableView;
}
@end
